Imports ComponentGo.Calendars

Imports Phoenix.DataAccess
Imports Phoenix.Events
Imports Phoenix.Helpers

Namespace Forms.Area

    Public Class CalendarAreaBase : Inherits AreaBase

        Public Event OnAppointmentSelected(ByVal sender As Object, ByVal args As EventArgs)
        Public Event FirstDateChanged(ByVal sender As Object, ByVal e As DateRangeEventArgs)

        Protected Overridable Sub OnFirstDateChanged(ByVal e As DateRangeEventArgs)
            RaiseEvent FirstDateChanged(Me, e)
        End Sub


#Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call
        End Sub

        'UserControl overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents ToolTipController As DevExpress.Utils.ToolTipController

        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.components = New System.ComponentModel.Container
            Me.ToolTipController = New DevExpress.Utils.ToolTipController(Me.components)
            '
            'ToolTipController
            '
            Me.ToolTipController.Style = New DevExpress.Utils.ViewStyle("ToolTip style")
            '
            'CalendarAreaBase
            '
            Me.Name = "CalendarAreaBase"

        End Sub

#End Region

#Region " Calendar Area Behaviour "

        Public Overrides Function CanCopy() As Boolean

            Return True
        End Function

        Public Overrides Function CanDelete() As Boolean
            Return True
        End Function

        Public Overrides Sub Paste(ByVal arr As IList)
            If arr Is Nothing Then
                Return
            End If
            For Each obj As DataRow In arr
                Dim item As DataAccess.DataSetAppointments.Appointment = Helpers.AppointmentHelper.Copy(obj)
                item.StartTime = CurrentDate
                Dim m As Integer = item.ExpectedDuration Mod 60
                Dim h As Double = item.ExpectedDuration / 60 - m / 60
                item.FinishTime = item.StartTime.AddHours(h)
                item.FinishTime = item.FinishTime.AddMinutes(m)
                Helpers.AppointmentHelper.UpdateAppointment(item, Me)
            Next
        End Sub

        Public Overrides Sub Delete(ByVal arr As IList)
            If arr Is Nothing Then Return
            If arr.Count = 0 Then Return

            AppointmentHelper.DeleteAppointments(arr, Me)
        End Sub

        Public Overrides Sub SelectAll()
            For Each app As Appointment In Calendar.Appointments
                app.Selected = True
            Next
            Calendar.Invalidate()
        End Sub

        Public Overrides Function Copy(ByVal arr As IList) As IList
            Dim list As New ArrayList
            For Each item As DataAccess.DataSetAppointments.Appointment In arr
                list.Add(item)
            Next
            Return list
        End Function

        Public Sub Reload()
            currFrom = Date.MinValue
            currTo = Date.MinValue
            Calendar.ReloadAppointments()
        End Sub


        Public Overridable Function SetPrev() As Date
        End Function
        Public Overridable Function SetNext() As Date
        End Function
        Public Overridable Function SetToday() As Date
        End Function
        Public Overridable Function SetDate(ByVal d As Date) As Date
        End Function


#End Region

#Region " ToolTips "
        Private mToolTipAppointment As ComponentGo.Calendars.Appointment
        Private mToolTipText As String
        Private mToolTipResourceIndex As Integer = -1

        Public Sub InitToolTip(ByVal p As Point)
            Dim app As Appointment = Calendar.HitAppointmentTest(p)

            If Not (app Is Nothing) Then
                If Not (app Is mToolTipAppointment) Then
                    ToolTipController.HideHint()
                    mToolTipAppointment = app

                    If app.Tag Is Nothing Then
                        ToolTipController.ShowHint(app.Text)
                    Else
                        ToolTipController.ShowHint(AppointmentHelper.GetAppointmentTooltip(CType(app.Tag, DataSetAppointments.Appointment)))
                    End If
                End If
            Else
                mToolTipAppointment = Nothing

                Dim text As String = GetCellToolTip(p)
                If text Is Nothing Then
                    mToolTipText = Nothing
                    ToolTipController.HideHint()
                Else
                    If text <> mToolTipText Then
                        mToolTipText = text
                        ToolTipController.ShowHint(mToolTipText, Cursor.Position)
                    Else
                        Dim index = Calendar.HitResourceTest(p)
                        If index <> mToolTipResourceIndex Then
                            mToolTipResourceIndex = index
                            ToolTipController.ShowHint(mToolTipText, Cursor.Position)
                        End If
                    End If
                End If
            End If
        End Sub

        Protected Overridable Function GetCellToolTip(ByVal p As Point) As String
            Return Nothing
        End Function
#End Region

#Region " Properties "
        Public Overridable Property FirstDate() As Date
            Get
                Return Now
            End Get
            Set(ByVal Value As Date)

            End Set
        End Property

        Public Overridable Property CurrentDate() As Date
            Get

                Dim dt As Date = Calendar.SelectedDateBegin
                If dt.Ticks = 0 Then dt = FirstDate

                Return dt
            End Get
            Set(ByVal Value As Date)
                FirstDate = Value
                Calendar.SelectedDateBegin = Value
                Calendar.SelectedDateEnd = Value
            End Set
        End Property

        Public Overrides ReadOnly Property SelectedItems() As IList
            Get
                Dim a As New ArrayList
                For Each app As Appointment In Calendar.Appointments
                    If app.Selected Then
                        a.Add(app.Tag)
                    End If
                Next
                Return a
            End Get
        End Property

        Public WithEvents Calendar As BaseCalendar

        Public Overridable ReadOnly Property HasResources() As Boolean
            Get
                Return True
            End Get

        End Property

        Public Overridable ReadOnly Property HasSchedule() As Boolean
            Get
                Return False
            End Get
        End Property


#End Region

#Region " Events Handlers"

        Private TimeSpanDay As TimeSpan = TimeSpan.FromHours(24)
        Private Sub Calendar_GetDayInfo(ByVal sender As Object, ByVal args As ComponentGo.Calendars.GetDayInfoEventArgs) Handles Calendar.GetDayInfo
            If DesignMode Then Return
            If Not HasSchedule Then Return

            args.WorkHourBegin = TimeSpan.Zero
            args.WorkHourEnd = TimeSpanDay
            args.Breaks.AddRange(mBreakManagers(args.Date.DayOfWeek).Breaks)

            For i As Integer = 0 To Calendar.Resources.Count - 1
                Dim re As Resource = Calendar.Resources(i)
                If Not CType(re.Tag, DataSetData.Employee).RowState = DataRowState.Detached Then
                    Dim rows() As DataSetData.EmployeeTime = AppDataLayer.EmployeeTimes.Select(String.Format( _
                        "EmployeeID={0} and DayOfWeek={1}", CType(re.Tag, DataSetData.Employee).EmployeeID, CInt(args.Date.DayOfWeek)))
                    If rows.Length <> 0 Then
                        Dim bm As BreakManager = New BreakManager(True)
                        Dim ri As GetDayInfoEventArgs.ResourceInfo = args.GetResourceInfo(i)
                        For j As Integer = 0 To rows.Length - 1
                            bm.AddBreak(TimeSpan.FromMinutes(rows(j).StartTime), TimeSpan.FromMinutes(rows(j).FinishTime))
                        Next

                        For Each sch As DataSetAppointments.BusyTime In AppDataLayer.GetAdditionalTimes(CType(re.Tag, DataSetData.Employee).EmployeeID, args.Date.Date, args.Date.Date.AddHours(24)).BusyTimes
                            bm.RemoveBreak(TimeSpan.FromMinutes(sch.StartTime), TimeSpan.FromMinutes(sch.FinishTime))
                        Next

                        For Each b As Break In bm.Breaks
                            b.Extra = AppConfig.AlternativeColorForEmployeeSchedule
                            ri.Breaks.Add(b)
                        Next

                        ri.Breaks.AddRange(mBreakManagers(args.Date.DayOfWeek).Breaks)
                    End If
                End If
            Next
        End Sub


        Private Sub Calendar_SelectionChanged(ByVal sender As Object, ByVal args As ComponentGo.Calendars.AppointmentEventArgs) Handles Calendar.SelectionChanged
            For Each ca As Appointment In Calendar.Appointments
                If ca.Selected And (ca.Tag Is Nothing) Then
                    ca.Selected = False
                End If
            Next
            RaiseEvent OnAppointmentSelected(Calendar, EventArgs.Empty)
        End Sub

        Private Sub Calendar_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Calendar.MouseMove
            Try
                InitToolTip(New Point(e.X, e.Y))
            Catch ex As Exception
                Debug.WriteLineIf(AppError, ex.ToString())
            End Try
        End Sub

        Private Sub CalendarAreaBase_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.MouseLeave
            ToolTipController.HideHint()
        End Sub


        Public currFrom As Date
        Public currTo As Date
        Private resFrom As Integer
        Private resTo As Integer

        Private Sub Calendar_RequestAppointments(ByVal sender As Object, ByVal args As RequestAppointmentsEventArgs) Handles Calendar.RequestAppointments
            If DesignMode Then Return

            If currFrom.CompareTo(args.DateFrom) <> 0 Or _
            currTo.CompareTo(args.DateTo) <> 0 Or _
            resFrom <> args.ResourceFrom Or _
            resTo <> args.ResourceTo Then
                LoadAppointments(args.DateFrom, args.DateTo)

                currFrom = args.DateFrom
                currTo = args.DateTo
                resFrom = args.ResourceFrom
                resTo = args.ResourceTo
            End If
        End Sub

        Private Sub CalendarAreaBase_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If DesignMode Then Return

            Calendar.AllowOverlap = True

            UpdateConfiguration()
            LoadResources()
            LoadSalonSchedule()

            SetDate(Date.Today)
            Calendar.SelectedDateBegin = Now
            Calendar.SelectedDateEnd = Now.AddMinutes(10)
        End Sub


        Private Sub Calendar_AppointmentChanged(ByVal sender As Object, ByVal args As AppointmentEventArgs) Handles Calendar.AppointmentChanged
            Dim dba As DataSetAppointments.Appointment = args.Appointment.Tag
            AppointmentHelper.CopyCalendarAppointment2DB(args.Appointment, dba)

            If HasResources Then
                Dim e As DataSetData.Employee = Calendar.Resources(args.Appointment.ResourceIndex).Tag
                dba.PerformerID = e.EmployeeID
                dba.ServiceCost = AppointmentHelper.ExpectedServiceCost(dba)
                dba.ExpectedDuration = AppointmentHelper.ExpectedServiceDuration(dba)
            End If

            Dim form As ChildFormBase = AppointmentForm.FindForm(dba)
            If Not form Is Nothing Then
                form.Activate()
                AppRaiseEvent(sender, New PhoenixEventArgs(dba, PhoenixEventCode.BizUpdated))
                Return
            ElseIf AppointmentHelper.GetAppointmentStatus(dba) = AppointmentStatus.asNew Then
                AppointmentForm.OpenForm(dba)
            ElseIf Not AppointmentHelper.CheckAndUpdateAppointment(dba, Me) Then
                If AppointmentHelper.GetAppointmentStatus(dba) = AppointmentStatus.asNew Then
                    Calendar.Appointments.Remove(args.Appointment)
                Else
                    dba.RejectChanges()
                    If HasResources Then
                        args.Appointment.ResourceIndex = FindResource(dba.PerformerID)
                    End If
                    AppointmentHelper.CopyDBAppointment2Calendar(dba, args.Appointment)
                End If
            End If
        End Sub

        Private Sub Calendar_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles Calendar.DragEnter
            Dim s As DataSetData.Service = e.Data.GetData(GetType(DataSetData.Service))

            If Not s Is Nothing Then
                Dim dba As DataSetAppointments.Appointment = AppointmentHelper.NewAppointment(s)
                dba.FinishTime = dba.StartTime.AddMinutes(30)
                Dim ca As Appointment = AppointmentHelper.CreateCalendarAppointment(dba)

                e.Data.SetData(ca)
            End If
        End Sub


        Private Sub Calendar_StartAppointmentDrag(ByVal sender As Object, ByVal e As StartAppointmentDragEventArgs) Handles Calendar.StartAppointmentDrag
            If e.Appointment.Tag Is Nothing Then
                e.Cancel = True
            ElseIf AppointmentHelper.GetAppointmentStatus(e.Appointment.Tag) = AppointmentStatus.asPaid Then
                e.Cancel = True
            End If
        End Sub

        Private Sub Calendar_QueryAppointmentDrag_CheckOpenForm(ByVal sender As Object, ByVal e As QueryAppointmentDragEventArgs) Handles Calendar.QueryAppointmentDrag
            Dim form As ChildFormBase = AppointmentForm.FindForm(e.Appointment.Tag)
            If Not form Is Nothing Then
                form.Activate()
            End If
        End Sub

        Private Sub Calendar_QueryAppointmentDrag_ShowHint(ByVal sender As Object, ByVal e As QueryAppointmentDragEventArgs) Handles Calendar.QueryAppointmentDrag
            ToolTipController.ShowHint(e.Appointment.DateBegin.ToString("t"), Cursor.Position)
        End Sub

        Private Sub Calendar_QueryAppointmentDrag_ChangeDuration(ByVal sender As Object, ByVal e As QueryAppointmentDragEventArgs) Handles Calendar.QueryAppointmentDrag
            Dim dba As DataSetAppointments.Appointment = e.Appointment.Tag

            If HasResources And (e.Appointment.ResourceIndex <> FindResource(dba.PerformerID)) Then
                dba.PerformerID = CType(Calendar.Resources(e.Appointment.ResourceIndex).Tag, DataSetData.Employee).EmployeeID
                dba.ExpectedDuration = AppointmentHelper.ExpectedServiceDuration(dba)
                dba.StartTime = e.Appointment.DateBegin
                dba.FinishTime = dba.StartTime.AddMinutes(dba.ExpectedDuration)

                AppointmentHelper.CopyDBAppointment2Calendar(dba, e.Appointment)
            End If
        End Sub


        Private Sub Calendar_Default(ByVal sender As Object, ByVal e As EventArgs) Handles Calendar.Default
            Dim apps As Object() = Calendar.Appointments.Selected
            If apps.Length = 0 Then Return

            For Each app As Appointment In apps
                If Not app.Tag Is Nothing Then
                    ChildFormBase.OpenForm(app.Tag)
                End If
            Next

        End Sub

        Private Sub Calendar_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Calendar.MouseUp
            If e.Button = MouseButtons.Right Then
                If Not Calendar.Appointments.FirstSelected Is Nothing Then
                    CType(AppMainWindow.ActiveView, View.MainView).OpenAppointmentBarItem.Enabled = True
                Else
                    CType(AppMainWindow.ActiveView, View.MainView).OpenAppointmentBarItem.Enabled = False
                End If
                ShowContextMenu()
            End If
        End Sub

        Private Sub Calendar_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Calendar.MouseDown
            CType(AppMainWindow.ActiveView, View.MainView).ActiveArea = Me

            If (e.Button = MouseButtons.Right) Then
                Dim p As Point = New Point(e.X, e.Y)

                Dim capp As ComponentGo.Calendars.Appointment = Calendar.HitAppointmentTest(p)
                Dim d As Date = Calendar.HitDateTimeTest(p)

                If capp Is Nothing Then
                    Calendar.SelectedDateBegin = d
                    Calendar.SelectedDateEnd = d.AddMinutes(10)

                    For Each a As ComponentGo.Calendars.Appointment In Calendar.Appointments
                        a.Selected = False
                    Next
                Else
                    Calendar.SelectedDateBegin = d
                    Calendar.SelectedDateEnd = d

                    If Not capp.Selected Then
                        For Each a As ComponentGo.Calendars.Appointment In Calendar.Appointments
                            If a Is capp Then
                                a.Selected = True
                            Else
                                a.Selected = False
                            End If
                        Next
                    End If
                End If

                If HasResources Then
                    Dim index As Integer = Calendar.HitResourceTest(p)
                    If index < 0 Then index = 0
                    If index >= Calendar.Resources.Count Then index = Calendar.Resources.Count - 1
                    If (index >= 0) And (index < Calendar.Resources.Count) Then
                        Calendar.SelectedResource = index
                    End If
                End If
            End If
        End Sub


#End Region

#Region " Application Level Event Handlers "
        Protected Overrides Sub OnApplicationEvent(ByVal sender As Object, ByVal e As Events.PhoenixEventArgs)
            If e.EventCode = PhoenixEventCode.ConfigurationChanged Then
                UpdateConfiguration()
            ElseIf e.EventCode = PhoenixEventCode.ScheduleChanged Then
                UpdateSchedule()
            ElseIf e.IsBizEvent Then
                If TypeOf e.EventData Is DataSetAppointments.Appointment Then
                    OnAppointmentEvent(e)
                ElseIf TypeOf e.EventData Is DataSetData.Service Then
                    OnServiceEvent(e)
                ElseIf TypeOf e.EventData Is DataSetData.Client Then
                    OnClientEvent(e)
                ElseIf TypeOf e.EventData Is DataSetData.Employee Then
                    OnEmployeeEvent(e)
                End If
            End If
        End Sub


        Private Sub OnServiceEvent(ByVal e As Events.PhoenixEventArgs)
            Dim service As DataSetData.Service = e.EventData
            Dim i As Long = 0
            While i < Calendar.Appointments.Count
                Dim ca As Appointment = Calendar.Appointments(i)
                Dim dba As DataSetAppointments.Appointment = CType(ca.Tag, DataSetAppointments.Appointment)
                If Not dba Is Nothing Then
                    If dba.ServiceID = service.ServiceID Then
                        Select Case e.EventCode
                            Case PhoenixEventCode.BizUpdated
                                AppointmentHelper.CopyDBAppointment2Calendar(dba, ca)
                            Case PhoenixEventCode.BizRemoved
                                Calendar.Appointments.RemoveAt(i)
                                i -= 1
                        End Select
                    End If
                End If
                i += 1
            End While
        End Sub

        Private Sub LoadResources()
            If Not HasResources Then Return

            Calendar.Resources.Clear()
            Dim dw As DataView = New DataView(AppDataLayer.Employees)
            dw.RowFilter = "IsAttendant=True"
            dw.Sort = "FullName ASC"

            For Each dr As DataRowView In dw
                Dim e As DataSetData.Employee = dr.Row
                Dim r As New Resource(e.FullName)
                r.Tag = e
                Calendar.Resources.Add(r)
            Next
        End Sub




        Private Sub OnEmployeeEvent(ByVal e As Events.PhoenixEventArgs)
            'LoadResources()
            'Reload()
            Select Case e.EventCode
                Case PhoenixEventCode.BizAdded
                    LoadResources()
                    Reload()
                Case PhoenixEventCode.BizRemoved
                    Dim resi = FindResource(CType(e.EventData, DataSetData.Employee).EmployeeID)
                    If resi <> -1 Then
                        Calendar.Resources.RemoveAt(resi)
                    End If
                    Reload()
                Case PhoenixEventCode.BizUpdated
                    'Dim resi = FindResource(CType(e.EventData, DataSetData.Employee).EmployeeID)
                    'If resi <> -1 Then
                    'Calendar.Resources(resi).Text = CType(e.EventData, DataSetData.Employee).FullName
                    'Else
                    'End If
                    LoadResources()
                    Reload()

            End Select
        End Sub



        Private Sub OnClientEvent(ByVal e As Events.PhoenixEventArgs)
            Dim client As DataSetData.Client = e.EventData
            Dim i As Long = 0
            While i < Calendar.Appointments.Count
                Dim ca As Appointment = Calendar.Appointments(i)
                Dim dba As DataSetAppointments.Appointment = CType(ca.Tag, DataSetAppointments.Appointment)
                If Not dba Is Nothing Then
                    If dba.ClientID = client.ClientID Then
                        Select Case e.EventCode
                            Case PhoenixEventCode.BizUpdated
                                AppointmentHelper.CopyDBAppointment2Calendar(dba, ca)
                            Case PhoenixEventCode.BizRemoved
                                Calendar.Appointments.RemoveAt(i)
                                i -= 1
                        End Select
                    End If
                End If
                i += 1
            End While
        End Sub


        Private Sub OnAppointmentEvent(ByVal e As PhoenixEventArgs)
            Dim app As DataSetAppointments.Appointment = CType(e.EventData, DataSetAppointments.Appointment)

            Select Case e.EventCode
                Case PhoenixEventCode.BizAdded
                    OnAppointmentAdded(app)

                Case PhoenixEventCode.BizRemoved
                    OnAppointmentRemoved(app)

                Case PhoenixEventCode.BizUpdated
                    OnAppointmentUpdated(app)
            End Select
        End Sub

        Private Sub OnAppointmentAdded(ByVal app As DataSetAppointments.Appointment)
            Dim ca As Appointment
            ca = FindAppointmentInCalendar(app)
            If ca Is Nothing Then
                ca = AppointmentHelper.CreateCalendarAppointment(app)
                If UpdateAppointmentResource(ca) Then Calendar.Appointments.Add(ca)
            Else
                AppointmentHelper.CopyDBAppointment2Calendar(app, ca)
                UpdateAppointmentResource(ca)
            End If
        End Sub

        Private Sub OnAppointmentRemoved(ByVal app As DataSetAppointments.Appointment)
            Dim ca As Appointment = Nothing
            If (app.RowState = DataRowState.Detached) Or (app.RowState = DataRowState.Deleted) Then
                ca = FindAppointmentInCalendar(app)
            Else
                ca = FindAppointmentInCalendar(app.AppointmentID)
            End If

            If Not ca Is Nothing Then
                Calendar.Appointments.Remove(ca)
            End If
        End Sub

        Private Sub OnAppointmentUpdated(ByVal app As DataSetAppointments.Appointment)
            Dim ca As Appointment = FindAppointmentInCalendar(app)
            If Not ca Is Nothing Then
                AppointmentHelper.CopyDBAppointment2Calendar(app, ca)
                UpdateAppointmentResource(ca)
            End If
        End Sub

        Private Function FindAppointmentInCalendar(ByVal app As DataSetAppointments.Appointment) As Appointment
            Return FindAppointmentInCalendar(app, True)
        End Function

        Private Function FindAppointmentInCalendar(ByVal app As DataSetAppointments.Appointment, ByVal checkById As Boolean) As Appointment
            For Each ca As Appointment In Calendar.Appointments
                If Not ca.Tag Is Nothing Then
                    If ca.Tag Is app Then
                        Return ca
                    End If

                    If checkById Then
                        If CType(ca.Tag, DataSetAppointments.Appointment).AppointmentID = app.AppointmentID Then
                            Return ca
                        End If
                    End If
                End If
            Next
            Return Nothing
        End Function

        Private Function FindAppointmentInCalendar(ByVal id As Integer) As Appointment
            For Each ca As Appointment In Calendar.Appointments
                If Not ca.Tag Is Nothing Then
                    If CType(ca.Tag, DataSetAppointments.Appointment).AppointmentID = id Then
                        Return ca
                    End If
                End If
            Next
            Return Nothing
        End Function
#End Region

#Region " Others "
        Private Sub UpdateSchedule()
            LoadSalonSchedule()
            Reload()
        End Sub


        Private Function UpdateAppointmentResource(ByVal ca As Appointment) As Boolean
            Dim resID As Integer = 0
            If HasResources Then
                resID = FindResource(CType(ca.Tag, DataSetAppointments.Appointment).PerformerID)
            End If
            If resID = -1 Then Return False

            ca.ResourceIndex = resID
            Return True
        End Function

        Protected Overridable Sub UpdateConfiguration()
            Calendar.SelectedAppointmentColor = AppConfig.SelectedAppointmentBackColor
            Calendar.SelectedAppointmentTextColor = AppConfig.SelectedAppointmentForeColor
            Calendar.WorkHourColor = AppConfig.WorkHourColor
            Calendar.WorkHourColorSecond = AppConfig.WorkHourColor2
            Calendar.FreeHourColor = AppConfig.FreeHourColor
            Calendar.HoursResolution = AppConfig.CalendarResolution
            Calendar.FreeHourColor = AppConfig.FreeHourColor
            'Calendar.ResourcesLabelColor = AppConfig.WorkHourColor
            Calendar.WorkHourColorSecond = AppConfig.WorkHourColor2
            Calendar.ExtraBreakHourColor = AppConfig.ExtraFreeHourColor
            Calendar.Invalidate()
        End Sub


        Private mBreakManagers(6) As BreakManager
        Private Sub LoadSalonSchedule()
            For i As Integer = 0 To 6
                mBreakManagers(i) = New BreakManager
            Next

            For Each st As DataSetData.SalonTime In AppDataLayer.SalonTimes
                mBreakManagers(st.DayOfWeek).AddWorkTime(TimeSpan.FromMinutes(st.StartTime), TimeSpan.FromMinutes(st.FinishTime))
            Next
        End Sub

        Private Sub LoadAppointments(ByVal sd As Date, ByVal ed As Date)
            Dim apps As DataSetAppointments.AppointmentsDataTable = AppDataLayer.GetAppointments(sd, ed).Appointments

            Calendar.Appointments.Clear()

            For Each app As DataSetAppointments.Appointment In apps
                Dim ca As Appointment = AppointmentHelper.CreateCalendarAppointment(app)
                If UpdateAppointmentResource(ca) Then
                    Calendar.Appointments.Add(ca)
                End If

            Next

            If HasSchedule Then
                LoadBreaks(sd, ed)
            End If

        End Sub

        Private Sub LoadBreaks(ByVal sd As Date, ByVal ed As Date)
            For Each sch As DataSetAppointments.BusyTime In AppDataLayer.GetBusyTimes(sd, ed).BusyTimes
                Dim resIndex As Long = FindResource(sch.EmployeeID)
                If resIndex <> -1 Then
                    Dim ca As Appointment = New Appointment
                    ca.ResourceIndex = resIndex

                    ca.DateBegin = sch.ExactDate.AddMinutes(sch.StartTime)
                    ca.DateEnd = sch.ExactDate.AddMinutes(sch.FinishTime)
                    ca.Text = sch.Descript

                    ca.BackColor = AppConfig.BreakBackColor
                    ca.ProgressColor = AppConfig.BreakBackColor
                    ca.ForeColor = AppConfig.BreakForeColor
                    ca.LineColor = AppConfig.BreakForeColor

                    Calendar.Appointments.Add(ca)
                End If
            Next
        End Sub

        Protected Function FindResource(ByVal id As Long) As Long
            For Each r As Resource In Calendar.Resources
                If CType(r.Tag, DataSetData.Employee).EmployeeID = id Then
                    Return Calendar.Resources.IndexOf(r)
                End If
            Next
            Return -1
        End Function


#End Region

        Protected Overrides Function AllowCopyItem(ByVal obj As Object) As Boolean
            If Not TypeOf obj Is DataSetAppointments.Appointment Then
                Return False
                Exit Function
            End If

            Dim app As DataSetAppointments.Appointment = obj

            If app.RowState = DataRowState.Detached Then
                Return False
            Else
                Return True
            End If
        End Function

        Protected Overrides Function AllowDeleteItem(ByVal obj As Object) As Boolean
            If Not TypeOf obj Is DataSetAppointments.Appointment Then
                Return False
                Exit Function
            End If

            Dim app As DataSetAppointments.Appointment = obj

            If app.RowState = DataRowState.Detached Then
                Return False
            ElseIf app.IsBillIDNull Then
                Return True
            Else
                Return False
            End If
        End Function

    End Class

End Namespace